/*
 *  NFCSigning - Open source library for signing/validation of NDEF messages
 *  Copyright (C) 2009-2010 The NFCSigning Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */
package org.nfcsigning.algorithm;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;

/**
 *
 * @author Markus Kilås
 */
public class DSASatsaVerifier implements SignatureVerifier {

    private static DSASatsaVerifier instance;
    
    private KeyFactory kf;
    private java.security.Signature sign;
    
    private DSASatsaVerifier(KeyFactory kf, java.security.Signature sign) {
        this.kf = kf;
        this.sign = sign;
    }
    
    public static DSASatsaVerifier getInstance() {
        if(instance == null) {
            try {
                KeyFactory kf = KeyFactory.getInstance("DSA");
                java.security.Signature sign = java.security.Signature.getInstance("SHA1withDSA");
                instance = new DSASatsaVerifier(kf, sign);
            } catch (NoSuchAlgorithmException ex) {
                return null;
            }
        }
        return instance;
    }
    
    public boolean verifySignature(byte[] signature, byte[] coveredBytes, PublicKey publicKey) throws SignatureException {
        try {
//            System.out.println("SATSA");
            SubjectPublicKeyInfo pkInfo = publicKey.getSubjectPublicKeyInfo();

            X509EncodedKeySpec pks = new X509EncodedKeySpec(pkInfo.getEncoded());
            
            sign.initVerify(kf.generatePublic(pks));
            sign.update(coveredBytes, 0, coveredBytes.length);

            return sign.verify(signature);
        } catch (java.security.SignatureException ex) {
            throw new SignatureException(ex);
        } catch (InvalidKeyException ex) {
            throw new SignatureException(ex, "InvalidKeyException: " + ex.getMessage());
        } catch (InvalidKeySpecException ex) {
            throw new SignatureException(ex, "InvalidKeySpecException: " + ex.getMessage());
        } catch (IOException ex) {
            throw new SignatureException(ex, "IOException: " + ex.getMessage());
        }
    }

}
